package com.cynoware.busappmas.model;


import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.PixelFormat;
import android.graphics.Point;
import android.graphics.PorterDuff;
import android.os.Handler;
import android.os.IBinder;
import android.os.Message;
import android.util.Log;
import android.view.Gravity;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.WindowManager;
/**
 * 
 * @author This service is intent for showing logo on screen while playing local video
 * Note: Please invoke it by startServie() and do not forget to stop it by stopService() 
 */
public class VideoLogoService extends Service {

	private final String TAG = "VideoLogoService";
	private int display_w = 1440;
    private int display_h = 900;
    private final String PATH_LOGO = "/data/logo.png";
    private Paint mPaint;
    private LoadView mView;
    private Bitmap mBitmap;

	private Handler mHandler = new Handler(){
		public void handleMessage(Message msg){
			mView.repaint();			
		}
	};
    private class LoadView extends SurfaceView implements SurfaceHolder.Callback {
    	
    	private SurfaceHolder surfaceHolder;
        LoadView(Context c) {
            super(c);

            mPaint = new Paint();
            mPaint.setAntiAlias(true);
            mPaint.setFilterBitmap(true);
            mPaint.setDither(true);

            surfaceHolder = this.getHolder();
            getHolder().setFormat(PixelFormat.TRANSLUCENT);
            surfaceHolder.addCallback(this);
        }

        @Override
        protected void onAttachedToWindow() {
            super.onAttachedToWindow();
        }

        @Override
        protected void onDetachedFromWindow() {
            super.onDetachedFromWindow();
        }

        @Override
        protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
            setMeasuredDimension(display_w,display_h);
        }

		public void repaint(){
           Canvas c = null;
           mBitmap = BitmapFactory.decodeFile(PATH_LOGO);
           
           if(mBitmap == null){
        	   Log.e(TAG,"Can't decode file "+PATH_LOGO+" into bitmap!");
        	   return;
           }
           float x = 0;
           float y = 0;
           try{
             c = surfaceHolder.lockCanvas();
             c.drawColor(Color.TRANSPARENT,PorterDuff.Mode.CLEAR);
             x = y = display_w/200;
             c.drawBitmap(mBitmap, x, y, mPaint);
           } finally {
              if(c != null){
                surfaceHolder.unlockCanvasAndPost(c);
              }
           }
        }
       public void surfaceDestroyed (SurfaceHolder holder){}
       public void surfaceCreated (SurfaceHolder holder){}
       public void surfaceChanged (SurfaceHolder holder, int format, int width, int height){}

    }
    
    @Override
    public void onCreate(){
    	super.onCreate();
      	
      	Point point = new Point();  
      	((WindowManager)getSystemService(WINDOW_SERVICE)).getDefaultDisplay().getRealSize(point); 
                
          display_w = point.x;
          display_h = point.y;    
        WindowManager.LayoutParams params = new WindowManager.LayoutParams(
              WindowManager.LayoutParams.MATCH_PARENT,
              WindowManager.LayoutParams.WRAP_CONTENT,
              WindowManager.LayoutParams.TYPE_SYSTEM_OVERLAY,
              WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE|
              WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE,
              PixelFormat.TRANSLUCENT);
          params.gravity = Gravity.START|Gravity.TOP;
          mView = new LoadView(this);
          WindowManager wm = (WindowManager)getSystemService(WINDOW_SERVICE);
          wm.addView(mView, params);
          mHandler.sendEmptyMessageDelayed(0, 2000);
    }

	@Override
	public IBinder onBind(Intent intent) {
		// TODO Auto-generated method stub
		return null;
	}
	
	@Override
	public void onDestroy(){
		super.onDestroy();
	}

}
